package com.egitim.util;
 
import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class UtilityMailClient {

	private     String SMTP_HOST_NAME = "smtp.gmail.com";
	private     String SMTP_PORT = "465";
	private     String emailMsgTxt = "";
	private     String emailSubjectTxt = "";
	private     String emailFromAddress = "kuruygu@gmail.com";
	private     String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	private     String[] sendTo = { "kuruygu@gmail.com" };
	private     String emailEncoding = "utf-8";
	MimeBodyPart messagePart = null;
	MimeBodyPart messagePartSubject = null;
	MimeMultipart multipart = null;

	public void sendSSLMessage(String recipients[], String subject,
			String message, String from) throws MessagingException {
		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("kuruygu@gmail.com", "M449862d");
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		
		//Setting the Email Subject	
	 
		 
		messagePart = new MimeBodyPart();
		 
		multipart = new MimeMultipart();
		multipart.addBodyPart(messagePart);  // adding message part
		
		//Setting the Email Encoding	
		messagePart.setText(message,emailEncoding);
		messagePart.setHeader("Content-Type","text/html; charset=\"utf-8\"");
		messagePart.setHeader("Content-Transfer-Encoding", "quoted-printable");
		
		 
		// Setting the Subject and Content Type
		 
		MimeMessage mimeMsg= new MimeMessage(session);
		mimeMsg.setSubject(subject,"utf-8");
        msg.setSubject(mimeMsg.getHeader("Subject")[0]);
		msg.setContent(multipart);
		Transport.send(msg);
	}

	public String getSMTP_HOST_NAME() {
		return SMTP_HOST_NAME;
	}

	public void setSMTP_HOST_NAME(String sMTP_HOST_NAME) {
		SMTP_HOST_NAME = sMTP_HOST_NAME;
	}

	public String getSMTP_PORT() {
		return SMTP_PORT;
	}

	public void setSMTP_PORT(String sMTP_PORT) {
		SMTP_PORT = sMTP_PORT;
	}

	public String getEmailMsgTxt() {
		return emailMsgTxt;
	}

	public void setEmailMsgTxt(String emailMsgTxt) {
		this.emailMsgTxt = emailMsgTxt;
	}

	public String getEmailSubjectTxt() {
		return emailSubjectTxt;
	}

	public void setEmailSubjectTxt(String emailSubjectTxt) {
		this.emailSubjectTxt = emailSubjectTxt;
	}

	public String getEmailFromAddress() {
		return emailFromAddress;
	}

	public void setEmailFromAddress(String emailFromAddress) {
		this.emailFromAddress = emailFromAddress;
	}

	public String getSSL_FACTORY() {
		return SSL_FACTORY;
	}

	public void setSSL_FACTORY(String sSL_FACTORY) {
		SSL_FACTORY = sSL_FACTORY;
	}

	public String[] getSendTo() {
		return sendTo;
	}

	public void setSendTo(String[] sendTo) {
		this.sendTo = sendTo;
	}
	
}
